package cn.iot.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.iot.common.dto.ResultMsg;
import cn.iot.sys.dao.SysClassroomDao;
import cn.iot.sys.dao.SysDormitoryDao;
import cn.iot.sys.dao.SysHostelDao;
import cn.iot.sys.entity.SysDormitory;
import cn.iot.sys.service.SysDormitoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class SysDormitoryServiceImpl implements SysDormitoryService {
    @Resource
    private SysDormitoryDao sysDormitoryDao;
    @Resource
    private SysClassroomDao sysClassroomDao;
    @Resource
    private SysHostelDao sysHostelDao;

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Override
    public ResultMsg getSysDormitoryList() {
        logger.info("SysDormitoryServiceImpl getSysDormitoryList start...");
        List<SysDormitory> sysDormitoryList = sysDormitoryDao.getSysDormitoryList();
        ResultMsg msg = new ResultMsg(200,"成功",sysDormitoryList);
        return msg;
    }

    @Override
    public ResultMsg addDormitory(SysDormitory sysDormitory) {
        logger.info("SysDormitoryServiceImpl addDormitory sysDormitory{}",sysDormitory);
        int count = sysDormitoryDao.addDormitory(sysDormitory);
        ResultMsg msg;
        if (count>0){
            msg = new ResultMsg(200,"成功",null);
            return msg;
        }
        msg = new ResultMsg(500,"失败",null);
        return msg;
    }

    @Override
    public ResultMsg getSysDormitory(Map<String, Object> map) {
        logger.info("SysDormitoryServiceImpl getSysDormitory msp{}",map);
        int pageSize = Integer.parseInt(String.valueOf(map.get("pageSize")));
        int pageNum = Integer.parseInt(String.valueOf(map.get("pageNum")));
        String locationName = (String)map.get("locationName");
        if (StrUtil.isNotEmpty(locationName)){
            pageNum = 1;
            map.remove("pageNum");
            map.put("pageNum",pageNum);
        }
        int start = (pageNum-1)*pageSize;
        map.remove("pageSize");
        map.put("pageSize",pageSize);
        map.put("start",start);
        int count = sysDormitoryDao.getSysDormitoryCount(map);
        int pageCount;
        if (count>0){
            pageCount = count%pageSize==0? count/pageSize:count/pageSize+1;
        }else {
            pageCount = 0;
        }
        List<SysDormitory> sysDormitoryList = sysDormitoryDao.getSysDormitory(map);
        map.put("sysDormitoryList",sysDormitoryList);
        map.put("pageCount",pageCount);
        ResultMsg msg = new ResultMsg(200,"成功",map);
        return msg;
    }

    @Override
    public ResultMsg getSysDormitoryCount(Map<String, Object> map) {
        logger.info("SysDormitoryServiceImpl getSysDormitoryCount msp{}",map);
        int count = sysDormitoryDao.getSysDormitoryCount(map);
        ResultMsg msg;
        if (count>0){
            msg = new ResultMsg(200,"成功",count);
            return msg;
        }
        msg = new ResultMsg(500,"失败",null);
        return msg;
    }

    @Override
    public ResultMsg updDormitoryById(SysDormitory sysDormitory) {
        logger.info("SysDormitoryServiceImpl updDormitoryById sysDormitory{}",sysDormitory);
        try {
            String locationName = sysDormitory.getLocationName();
            SysDormitory sysDorByLocationName = sysDormitoryDao.getSysDorByLocationName(locationName);
            if (sysDorByLocationName != null){
                return new ResultMsg(500,"位置名称重复",null);
            }
            int sysClassroomByDorId = sysClassroomDao.getSysClassroomByDorId(sysDormitory.getId());
            int sysHostelByDormitoryId = sysHostelDao.getSysHostelByDormitoryId(sysDormitory.getId());
            if (sysHostelByDormitoryId>0 || sysClassroomByDorId>0){
                return new ResultMsg(500,"该楼栋正在使用,不可修改",null);
            }
            int count = sysDormitoryDao.updDormitoryById(sysDormitory);
            if (count>0){
                return new ResultMsg(200,"成功",null);
            }
            return new ResultMsg(500,"失败",null);
        } catch (Exception e){
            return new ResultMsg(500,"失败",null);
        }
    }

    @Override
    public ResultMsg getSysDorByLocationName(String locationName) {
        logger.info("SysDormitoryServiceImpl getSysDorByLocationName locationName{}",locationName);
        SysDormitory sysDorByLocationName = sysDormitoryDao.getSysDorByLocationName(locationName);
        ResultMsg msg;
        if (sysDorByLocationName != null){
            msg = new ResultMsg(200,"成功",sysDorByLocationName);
            return msg;
        }
        msg = new ResultMsg(500,"失败",null);
        return msg;
    }

    @Override
    public ResultMsg delSysDormitoryById(int id) {
        logger.info("SysDormitoryServiceImpl delSysDormitoryById id{}",id);
        ResultMsg msg;
        try {
            int sysClassroomByDorId = sysClassroomDao.getSysClassroomByDorId(id);
            int sysHostelByDormitoryId = sysHostelDao.getSysHostelByDormitoryId(id);
            if (sysHostelByDormitoryId>0 || sysClassroomByDorId>0){
                msg = new ResultMsg(500,"该楼栋正在使用,不可删除",null);
                return msg;
            }
            int count = sysDormitoryDao.delSysDormitoryById(id);
            if (count>0){
                msg = new ResultMsg(200,"成功",null);
                return msg;
            }
            msg = new ResultMsg(500,"失败",null);
            return msg;
        } catch (Exception e){
            msg = new ResultMsg(500,"失败",null);
            return msg;
        }
    }
}
